// SetSysConfig.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "SetSysConfig.h"
#include "SetSysConfigDlg.h"
#include "../gywa/Common/CommonFun.h"
#include "../gywa/Common/CommonDef.h"
#include <setupapi.h>
#include "usbiodef.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSetSysConfigApp

BEGIN_MESSAGE_MAP(CSetSysConfigApp, CWinApp)
	//{{AFX_MSG_MAP(CSetSysConfigApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSetSysConfigApp construction

CSetSysConfigApp::CSetSysConfigApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CSetSysConfigApp object

CSetSysConfigApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CSetSysConfigApp initialization

static struct
{
	TCHAR szPermitUSBDev[MAX_PATH * 2];
}
s_PermitDevices[] = 
{
	_T("8C448EE6A0FF0BDCCB54736EABC6A98EDFCCA91C7F2A183D6E73E26DA8FB4A48391B739833EAD82D67B45405B07BC989E7E964A6ACD7014F0B4827FA9FF74E223FFC5FCCBFC7977C51B863D74B91853748A218B302DE4AFC7F881705"),
	_T("8C448EE6A0FF0BDCCB54736EABC6A98E4FCC991C2F2A383D6E73E26DA8FB4A48391B739853EAF82DC0B45405B07BC989E7E964A6ACD7014F0B4827FA9FF74E223FFC5FCCAFC7B77C16B863D7"),
	_T("8C448EE6A0FF0BDCCB54736EABC6A98E4FCC991C2F2A383D6E73E26DA8FB4A48391B739853EAF82DC0B45405B07BC989E7E964A6ACD7014F0B4827FA9FF74E223FFC5FCC9FC7D07C71B833D7"),
};


BOOL CSetSysConfigApp::InitInstance()
{
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	CString strExePath = GetExePath();
	strExePath = strExePath.Left(3);

	if(GetDriveType(strExePath) != DRIVE_REMOVABLE)
	{
		GYMsgBox(IDS_SYS_RUN_ON_REMOVABLE, MB_ICONSTOP);
		return FALSE;
	}

	if(!CheckPermitUSBDev())
	{
		GYMsgBox(IDS_SYS_NOT_PERMITTED, MB_ICONSTOP);
		return FALSE;
	}

	CSetSysConfigDlg dlg;
	m_pMainWnd = &dlg;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}

BOOL CSetSysConfigApp::CheckPermitUSBDev()
{
	HDEVINFO hDevInfo = SetupDiGetClassDevs((LPGUID)&GUID_CLASS_USB_DEVICE, 0, 0, (DIGCF_PRESENT|DIGCF_DEVICEINTERFACE)); 
	if (hDevInfo == INVALID_HANDLE_VALUE) 
		return FALSE; 

	BOOL bVerifyPass = FALSE;

	SP_DEVINFO_DATA DeviceInfoData = {0}; 
	DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); 
	for (int j=0; SetupDiEnumDeviceInfo(hDevInfo, j, &DeviceInfoData); j++) 
	{ 
		DWORD nSize = 0; 
		TCHAR buf[MAX_PATH] = {0}; 
		if ( !SetupDiGetDeviceInstanceId(hDevInfo, &DeviceInfoData, buf, sizeof(buf), &nSize) ) 
			continue; 
		
		CString strEncrypt = EncryptString(buf);
		for(int i = 0; i < ARRAY_ELE_COUNT(s_PermitDevices); i++)
		{
			if(strEncrypt.CompareNoCase(s_PermitDevices[i].szPermitUSBDev) == 0)
			{
				bVerifyPass = TRUE;
				break;
			}
		}

		if(bVerifyPass)
			break;
	} 
	
	SetupDiDestroyDeviceInfoList(hDevInfo);	
	return bVerifyPass;
}
